package com.scisoft.scan.control.check;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.scisoft.scan.control.ClearEditText;
import com.scisoft.scan.dy.R;
import com.scisoft.scan.control.MainMenuActivity;
import com.scisoft.scan.data.check.CheckOrderBean;
import com.scisoft.scan.util.CommonUtil;
import com.scisoft.scan.ws.WebServiceHelper_Check;

import java.util.ArrayList;
import java.util.List;

public class CheckOrderActivity extends Activity {
    private ImageView iv_Back; //返回按钮
    private ImageView iv_query; //搜索按钮
    private TextView bt_packed;
    private TextView bt_packing;
    private TextView bt_unpack;
    private SwipeRefreshLayout mRefreshLayout;
    private ListView lv_data;
    private View v_packed;
    private View v_packing;
    private View v_unpack;
    private ClearEditText et_bar;
    //全部订单
    private List<CheckOrderBean> checkOrderBeanList;
    //当前过滤后的订单，列表操作的是这个
    private List<CheckOrderBean> filterCheckOrderBeanList;
    private String bizId;// 1 未盘 2 在盘 3 已审

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//即隐藏标题栏
        setContentView(R.layout.activity_check_order);

        //下拉刷新控件
        mRefreshLayout = findViewById(R.id.swipe_layout);
        mRefreshLayout.setProgressViewEndTarget(true,0);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRefreshLayout.setRefreshing(false);
                QueryData();
            }
        });
        lv_data = findViewById(R.id.lv_data);
        lv_data.setOnItemClickListener(new ListViewClick());
        bt_packed = findViewById(R.id.bt_packed);
        bt_packing = findViewById(R.id.bt_packing);
        bt_unpack = findViewById(R.id.bt_unpack);
        v_packed = findViewById(R.id.v_pack);
        v_packing = findViewById(R.id.v_packing);
        v_unpack = findViewById(R.id.v_unpack);
        et_bar = findViewById(R.id.et_bar);
        et_bar.setText("");
        //页签切换
        bt_packed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButton(v);
                QueryData();
            }
        });
        bt_packing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButton(v);
                QueryData();
            }
        });
        bt_unpack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButton(v);
                QueryData();
            }
        });
        //返回
        iv_Back = findViewById(R.id.iv_back);
        iv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(CheckOrderActivity.this, MainMenuActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivity(intent);
                finish();
            }
        });
        //搜索框
        iv_query = findViewById(R.id.iv_query);
        iv_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                filterData(et_bar.getUpperText().toString());
                BindData(CheckOrderActivity.this);
            }
        });
        setButton(bt_unpack);
        QueryData();


    }

    //设置tab页的颜色
    private void setButton(View v) {
        if (v.getId() == bt_packed.getId()) {
            bizId = "3";//已盘
            v_packed.setVisibility(View.VISIBLE);
            v_packing.setVisibility(View.INVISIBLE);
            v_unpack.setVisibility(View.INVISIBLE);
            bt_packed.setTextColor(getResources().getColor(R.color.tab_text_color_select));
            bt_packing.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_unpack.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
        } else if (v.getId() == bt_packing.getId()) {
            bizId = "2";//在盘
            v_packed.setVisibility(View.INVISIBLE);
            v_packing.setVisibility(View.VISIBLE);
            v_unpack.setVisibility(View.INVISIBLE);
            bt_packed.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_packing.setTextColor(getResources().getColor(R.color.tab_text_color_select));
            bt_unpack.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
        } else {
            bizId = "1";//未盘
            v_packed.setVisibility(View.INVISIBLE);
            v_packing.setVisibility(View.INVISIBLE);
            v_unpack.setVisibility(View.VISIBLE);
            bt_packed.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_packing.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_unpack.setTextColor(getResources().getColor(R.color.tab_text_color_select));
        }

    }
    //进入二级界面
    private class ListViewClick implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(CheckOrderActivity.this, CheckDetailActivity.class);
            if (position >= 0 && position < filterCheckOrderBeanList.size()) {
                CheckOrderBean checkOrderBean = filterCheckOrderBeanList.get(position);
                intent.putExtra("data", checkOrderBean);
                //审核状态
                String checkState = "0";
                if (bizId.equals("3")) {
                    checkState = "1";
                }
                intent.putExtra("checkState", checkState);
                startActivityForResult(intent, 0);
            }
        }
    }
    //返回当前界面
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (bizId) {
            case "1":
                setButton(bt_unpack);
                break;
            case "2":
                setButton(bt_packing);
                break;
            case "3":
                setButton(bt_packed);
                break;
        }
        QueryData();
    }
    //过滤(配件列表包含过滤条件)
    private void filterData(String filter) {
        filterCheckOrderBeanList = new ArrayList<>();
        if (filter != null && filter != "") {
            if (checkOrderBeanList != null) {
                for (int i = 0; i < checkOrderBeanList.size(); i++) {
                    CheckOrderBean bean = checkOrderBeanList.get(i);
                    if (bean.orderNo.contains(filter)) {
                        filterCheckOrderBeanList.add(bean);
                    }
                }
            }
        } else {
            if (checkOrderBeanList != null) {
                for (int i = 0; i < checkOrderBeanList.size(); i++) {
                    CheckOrderBean bean = checkOrderBeanList.get(i);
                    filterCheckOrderBeanList.add(bean);
                }
            }
        }
    }
    //绑定数据
    private void BindData(Context context) {
        CheckOrderAdapter adapter = new CheckOrderAdapter(filterCheckOrderBeanList, context);
        lv_data.setAdapter(adapter);
    }
    //查询数据
    private void QueryData() {
        CommonUtil.showPress(this);
        QueryTask queryTask = new QueryTask(CheckOrderActivity.this);
        queryTask.execute();
    }
    //查询服务
    class QueryTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        QueryTask(Context context) {
            super();
            mcontext = context;
        }
        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                checkOrderBeanList = WebServiceHelper_Check.CHECK_01(bizId);
                msg = "成功";

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }
        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                filterData(et_bar.getUpperText().toString());
                BindData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

}
